package com.example.kt_base.s6

//TODO 110.Kotlin语言的out -协变


//生产者 out T 协变
interface Producer<out T>{
    //out T 代表整个生产者类里面 这个T只能读取，不能被修改了
  //  fun consumer(itme:T)

    fun producer():T

}

//消费者 in T 逆变
interface Consumer<in T>{
    fun consumer(itme:T)
}

//生产者&消费者 T 默认情况下，是不变
interface ProducerAndConsumer<T>{
    fun producer():T

    fun consumer(itme:T)
}

open class Animal
open class Humanity:Animal()
open class Man:Humanity()
open class WoMan:Humanity()

class ProducerClass1:Producer<Animal>{
    override fun producer(): Animal {
       println("生产者 Animal")
        return Animal()
    }
}

class ProducerClass2:Producer<Humanity>{
    override fun producer(): Humanity {
        println("生产者 Humanity")
        return Humanity()
    }
}

class ProducerClass3:Producer<Man>{
    override fun producer(): Man {
        println("生产者 Man")
        return Man()
    }
}

class ProducerClass4:Producer<WoMan>{
    override fun producer(): WoMan {
        println("生产者 WoMan")
        return WoMan()
    }
}

fun main() {
    val p1:Producer<Animal> =ProducerClass1()
    val p2:Producer<Animal> =ProducerClass2()
    val p3:Producer<Animal> =ProducerClass3()
    val p4:Producer<Animal> =ProducerClass4()
    p1.producer()
    p2.producer()
    p3.producer()
    p4.producer()
}